#include <windowsx.h>
#include <string.h>
#include "wchar.h"

#include "dxdiag_res.h"
#include "dxdiagcom.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);

/* Display tab callback proceedure */
LRESULT CALLBACK Display_DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HBRUSH g_hbrBackground = CreateSolidBrush(RGB(255,255,255));
    switch(uMsg) {
        case WM_INITDIALOG:
            return TRUE;
        case WM_CTLCOLORSTATIC:
            if (GetDlgCtrlID((HWND) lParam) == DISPLAYTAB_NOTESEDIT) {
                return (LRESULT)g_hbrBackground;
            }
          case WM_COMMAND:
                if (LOWORD(wParam) == DISPLAYTAB_DDRAWTEST) {
                    DDrawTest_testDDraw(); /* TODO: Only one window at a time! Timeout? */
                }
                if (LOWORD(wParam) == DISPLAYTAB_D3DTEST) {
                    D3DTest_testD3D(); /* TODO: Animation not showing up! Timeout? */
                }
                break;
        default:
            break;
    }

    return FALSE;
}

BOOL DisplayInfo_GetInfo(DxDiagCOM* pcom, DisplayInfo* info)
{
    IDxDiagContainer* displayEnum;
    IDxDiagContainer* display;

    /* Get the display devices container */
     if (FAILED(DxDiagCOM_GetContainer(pcom->root, (LPCWSTR)("DxDiag_DisplayDevices"), &displayEnum)))
        return FALSE;
    /* Get the primary display device container */
     if (FAILED(DxDiagCOM_GetContainer(displayEnum, (LPCWSTR)("0"), &display)))
        return FALSE;

    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDESCRIPTION), &info->device.name);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZMANUFACTURER), &info->device.manufacturer);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZCHIPTYPE), &info->device.chipType);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDACTYPE), &info->device.DACType);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZKEYDEVICEID), &info->device.keyID);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDISPLAYMEMORYENGLISH), &info->device.memory);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDISPLAYMODEENGLISH), &info->device.displayMode);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZMONITORNAME), &info->device.monitor);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZMONITORMAXRES), &info->device.maxResolution);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDRIVERNAME), &info->driver.filename);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDRIVERVERSION), &info->driver.version);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDRIVERDATELOCALIZED), &info->driver.date);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZMINIVDD), &info->driver.minivdd);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZMINIVDDDATEENGLISH), &info->driver.minivddDate);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZVDD), &info->driver.vdd);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDDIVERSIONLOCALIZED), &info->driver.ddiVersion);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDRIVERATTRIBUTES), &info->driver.attributes);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDRIVERLANGUAGELOCALIZED), &info->driver.language);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDEVICEIDENTIFIER), &info->device.identifier);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZVENDORID), &info->driver.vendorID);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZDEVICEID), &info->driver.deviceID);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZSUBSYSID), &info->driver.subsystemID);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZREVISIONID), &info->driver.revisionID);
    DxDiagCOM_GetStringProperty(display, DxDiag_LoadString(DISPLAY_SZNOTESENGLISH), &info->notes);

    return TRUE;
}

void Display_DisplayData()
{
    HWND hWnd;

    /* Disable the acceleration controls (not implemented) */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_DDRAWBTN);
    Button_Enable(hWnd, FALSE);
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_D3DBTN);
    Button_Enable(hWnd, FALSE);
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_AGPBTN);
    Button_Enable(hWnd, FALSE);

    /* Display the device name */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_NAMEDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.name);

    /* Display the device manufacturer */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_MFGDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.manufacturer);

    /* Display the device chip type */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_CHIPDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.chipType);

    /* Display the device DAC type info */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_DACDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.DACType);

    /* Display the device memory info */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_MEMDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.memory);

    /* Display the device mode */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_MODEDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.displayMode);

    /* Display the monitor name */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_MONITORDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.device.monitor);

    /* Display the driver filename */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_DRIVERDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.driver.filename);

    /* Display the driver version */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_VERSIONDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.driver.version);

    /* Display the driver date */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_DATEDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.driver.date);

    /* Display the driver mini vdd */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_MINIDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.driver.minivdd);

    /* Display the driver vdd */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_VDDDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.driver.vdd);

    /* Display the driver ddi version */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_DDIDATA);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.driver.ddiVersion);

    /* Display the notes */
    hWnd = GetDlgItem(hTabDisplay, DISPLAYTAB_NOTESEDIT);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)displayInfo.notes);
}

void Display_SaveText(FILE* pFile)
{
   /* XXX fixme: not implemented */ 
}

void Display_SaveXML(XML_Node* root)
{
    XML_Node* displayXML;

    displayXML = XML_insertChildNode(root, (LPCWSTR)(DxDiag_LoadString(DISPLAY_XML_DISPLAYDEVICE)), NULL);

    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_CARDNAME), displayInfo.device.name);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_MANUFACTURER), displayInfo.device.manufacturer);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_CHIPTYPE), displayInfo.device.chipType);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DACTYPE), displayInfo.device.DACType);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DEVICEKEY), displayInfo.device.keyID);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DISPLAYMEMORY), displayInfo.device.memory);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_CURRENTMODE), displayInfo.device.displayMode);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_MONITOR), displayInfo.device.monitor);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_MONITORMAXRES), displayInfo.device.maxResolution);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERNAME), displayInfo.driver.filename);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERVERSION), displayInfo.driver.version);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERLANGUAGE), displayInfo.driver.language);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DDIVERSION), displayInfo.driver.ddiVersion);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERBETA),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERDEBUG),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERDATE),displayInfo.driver.date);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DRIVERWHQLLOGO),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_WHQLDATESTAMP),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_VDD),displayInfo.driver.vdd);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_MINIVDD),displayInfo.driver.minivdd);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_MINIVDDDATE),displayInfo.driver.minivddDate);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_MINIVDDSIZE),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DEVICEIDENTIFIER),displayInfo.device.identifier);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_VENDORID),displayInfo.driver.vendorID);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DEVICEID),displayInfo.driver.deviceID);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_SUBSYSID),displayInfo.driver.subsystemID);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_REVISIONID),displayInfo.driver.revisionID);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DXVAMODES),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_REGISTRY),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DDRAWSTATUS),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_D3DSTATUS),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_AGPSTATUS),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_DDRAWTESTRESULT),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_D3D7TESTRESULT),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_D3D8TESTRESULT),NULL);
    XML_insertChildNode(displayXML, (LPCWSTR)DxDiag_LoadString(DISPLAY_XML_D3D9TESTRESULT),NULL);
}
